package controllers

import (
	"cm2/exception"
	"cm2/service/irisweb"
	"cm2/terminal/console/win/webserv/model"

	"cm2/service"

	"github.com/kataras/iris"
)

// RequestTask 请求任务控制器
func RequestTask(ctx iris.Context, app *irisweb.Webapp) (interface{}, *exception.Exception) {
	var req model.JobRequest
	irisweb.ReadJSON(ctx, &req)

	// 解析Job
	job, err := service.DefaultJobService().ResolveJob(req.JobSId, req.Params)
	if err != nil {
		return nil, err
	}

	// 发送数据包
	err = service.DefaultTaskServer().BeginSendJob(req.WorkerSId, job)
	if err != nil {
		return nil, err
	}

	return job, nil
}
